var t = require("@babel/runtime/helpers/interopRequireDefault").default;

require("@babel/runtime/helpers/Arrayincludes");

var e = t(require("./utils/appconfig.js")), o = t(require("./utils/api.js")), n = t(require("./utils/port.js")), a = require("/libs/bmap/bmap-wx.min.js");

wx.onPageNotFound(function(t) {
    wx.reLaunch({
        url: "/pages/homepages/homepages"
    });
}), App({
    onLaunch: function(t) {
        console.log("小程序启动onLaunch:", t), this.initUpdateManager(), this.setOrderCode(t), 
        this.validationLogin(t), this.getUserInfo(), this.getOpenId();
    },
    initUpdateManager: function() {
        var t = wx.getUpdateManager();
        t.onCheckForUpdate(function(t) {}), t.onUpdateReady(function() {
            wx.showModal({
                title: "更新提示",
                content: "更新提示",
                success: function(e) {
                    e.confirm && t.applyUpdate();
                }
            });
        }), t.onUpdateFailed(function() {
            wx.showModal({
                title: "已有新版本",
                content: "请您删除当前小程序，重新打开",
                showCancel: !1
            });
        });
    },
    globalData: {
        mapKey:'YW7BZ-PYXL5-CU5I2-IP5NT-QET6O-SAF4V',
        userInfo: null, 
        baseUrl: "https://testtj1.zrs360.vip/api/",
        pathUrl: "https://testtj1.zrs360.vip/",
        baseImgUrl: "https://testtj1.zrs360.vip/public/uploads/image/",
        basevideoUrl: "https://testtj1.zrs360.vip/public/uploads/video/",
        appInfo: e.default.getAppInfo(),
        token: "",
        keyTjgj: {
            disCode: "",
            classify: ""
        },
        ifSharebtn: 0,
        selectedCity: "北京",
        selectedCityAreaCode: "010",
        selectedCityCode: "110000",
        gdt_vid: null,
        locationCityInfo: {
            isFirstLoacation: !1,
            selectedCity: "北京",
            selectedCityAreaCode: "010",
            localLatitude: "",
            localLongitude: "",
            localCity: "北京",
            localCityAreaCode: "010"
        },
        currentCityInfo: {},
        testName: [ "17625010775", "2" ],
        jumpPkgList: {
            isJumpPkgList: !1,
            requestParamsUrl: "",
            areaCode: ""
        },
        jumpHospitalList: {
            isJumpHospitalList: !1,
            requestParamsUrl: "",
            areaCode: ""
        },
        redEnvelopeList: [],
        userInfo: null,
        isFirstLoacation: !0,
        promotionSourceCode: null,
        base: n.default,
        api: o.default,
        coupon: [],
        openId: null,
        systemNavInfo: null,
        systemInfo: null
    },
    getDisUrl: function(t, e) {
        console.log(t, e);
        var o = wx.getStorageSync("disCode");
        o = o || this.globalData.keyTjgj.disCode, e.dis = o;
        var n = "";
        for (var a in e) e[a] && (n = n.length > 1 ? "".concat(n, "&").concat(a, "=").concat(e[a]) : "".concat(a, "=").concat(e[a]));
        var i = "";
        if (wx.canIUse("getAccountInfoSync")) {
            var s = wx.getAccountInfoSync().miniProgram.envVersion;
            "develop" === s ? i = "http://11:3017" : "trial" === s ? i = "http://192.168.1.202:3017" : "release" === s && (i = "https://m.viptijian.com");
        }
        return "".concat(i, "/").concat(t, "?").concat(n);
    },
    ifShowBtn: function() {
        var t = wx.getStorageSync("classify");
        return !(!t || "1B10" != t);
    },
    initShare: function(t) {
        var e = t.disCode, o = t.token, n = t.classify, a = t.userId;
        if (n && "1B10" == n && (this.globalData.token = o, this.globalData.keyTjgj.disCode = e, 
        this.globalData.keyTjgj.classify = n, wx.setStorageSync("token", o), wx.setStorageSync("disCode", e), 
        wx.setStorageSync("classify", n), wx.setStorageSync("userId", a)), e) {
            var i = wx.getStorageSync("classify");
            null != i && "1B10" == i || (this.globalData.keyTjgj.disCode = e, wx.setStorageSync("disCode", e));
        }
    },
    setOrderCode: function(t) {
        t.query.hasOwnProperty("prosc") && [ "pages/homepages/homepages", "pages/hospital-list/hospital-list", "pages/hospitalinfo/hospitalinfo", "pages/packagelists/packagelists" ].includes(t.path) && (this.globalData.promotionSourceCode || (this.globalData.promotionSourceCode = t.query.prosc));
    },
    validationLogin: function(t) {
        for (var e = [ "pages/usercenter/userOrder", "pages/usercenter/user_record", "pages/usercenter/user_property", "pages/usercenter/user_service", "pages/usercenter/user_report", "pages/usercenter/user_property/user_discounts/user_discounts" ], n = 0; n < e.length; n++) {
            if (-1 != t.path.indexOf(e[n])) return void o.default.getPort("information").then(function(t) {}, function(t) {
                wx.navigateTo({
                    url: "/pages/login/login"
                });
            });
        }
    },
    getOpenId: function() {
        var t = this, e = this.globalData.appInfo.zkPlat, n = this.globalData.appInfo.appType;
        return new Promise(function(a, i) {
            wx.login({
                success: function(s) {
                    if (s.code) {
                        var r = s.code, c = {
                            zkPlat: e,
                            appType: n,
                            code: r
                        };
                        // 获取token openid st
                        console.log("kais获取openid",c)
                     
                        // postPort
                        o.default.post1("wechat/Apiwechat/getOpenid", c, !1).then(function(e) {
                            console.log("获取openid",e)
                            "1" == e.code && (e.data ? (console.log(e.data), t.globalData.openId = e.data.openid, 
                            wx.setStorageSync("openid",e.data.openid),wx.setStorageSync("openid",e.data.openid), 
                            wx.setStorageSync("unionId", e.data.session_key),wx.setStorageSync("sessionKey", e.data.session_key),
                            a({
                                code: 1,
                                msg: "成功"
                            })) : i({
                                code: -1,
                                msg: "失败"
                            }));
                        }, function(t) {
                            console.log(t), wx.showToast({
                                title: "wx.login失败！",
                                icon: "none"
                            }), i({
                                code: -1,
                                msg: "失败"
                            });
                        });


                        
                    } else wx.showToast({
                        title: "wx.login失败！" + s.errMsg,
                        icon: "none"
                    }), i({
                        code: -1,
                        msg: "失败"
                    });
                }
            });
        });
    },
    getLocate: function() {
        return new Promise(function(t, e) {
            wx.getSetting({
                success: function(o) {
                    o.authSetting["scope.userLocation"] ? t() : wx.authorize({
                        scope: "scope.userLocation",
                        success: function(e) {
                            t();
                        },
                        fail: function(o) {
                            console.log("wx.authoriz失败"), wx.showModal({
                                title: "是否授权当前位置",
                                content: "需要获取您的地理位置，请确认授权，否则地图功能将无法使用",
                                success: function(o) {
                                    o.confirm ? wx.openSetting({
                                        success: function(o) {
                                            !0 === o.authSetting["scope.userLocation"] ? (wx.showToast({
                                                title: "获取位置授权成功",
                                                icon: "success",
                                                duration: 1e3
                                            }), t()) : e();
                                        }
                                    }) : e();
                                }
                            });
                        }
                    });
                },
                fail: function(t) {
                    console.log("wx.getSetting失败"), e();
                }
            });
        });
    },
    firstLocation: function() {
        console.log("定位")
        var t = this;
        return new Promise(function(e, o) {
            var n = t;
            n.getLocate().then(function(t) {
                new a.BMapWX({
                    ak: n.globalData.appInfo.bMapAK
                }).regeocoding({
                    success: function(t) {
                        var o = t.wxMarkerData, a = t.originalData, i = n.cutString(a.result.addressComponent.province, [ "省", "市" ]), s = n.cutString(a.result.addressComponent.city, [ "市" ]), r = o[0].latitude, c = o[0].longitude;
                        e({
                            province: i,
                            city: s,
                            latitude: r,
                            longitude: c
                        });
                    },
                    fail: function(t) {
                        console.log(t), wx.showModal({
                            title: "温馨提示",
                            content: "定位失败,请手动定位"
                        }), o("定位失败");
                    }
                });
            }, function(t) {
                console.log(t), wx.showModal({
                    title: "温馨提示",
                    content: "定位失败,请手动定位"
                }), o("定位失败");
            });
        });
    },
    cutString: function(t, e) {
        for (var o = 0; o < e.length; o++) {
            var n = t.lastIndexOf(e[o]);
            n > 0 && (t = t.substr(0, n));
        }
        return t;
    },
    getCityCode: function(t) {
        var e = this;
        return new Promise(function(o, n) {
            var a = e, i = {
                province: t.province,
                city: t.city
            };
            a.globalData.api.post1("wechat/home/getlocalcityinfo", i, !1).then(function(e) {
                if ("1" == e.code) {
                    var i = e.data.areacode;
                    a.globalData.localCity = t.city, a.globalData.localCityAreaCode = i, a.globalData.localLatitude = t.latitude, 
                    a.globalData.localLongitude = t.longitude, a.globalData.selectedCity = t.city, a.globalData.selectedCityAreaCode = i;
                    var s = {
                        city: t.city,
                        areaCode: i,
                        latitude: t.latitude,
                        longitude: t.longitude
                    };
                    o(s);
                } else console.log("获取城市code失败:" + e), n();
            }, function(t) {
                console.log("获取城市code失败:" + t), n();
            });
        });
    },
    getUserInfo: function() {
        var t = this;
        this.globalData.api.post1("information").then(function(e) { 
            if ("SUCCESS" == e.result) {
                var o = e.values;
                t.globalData.userInfo = o;
            }
        }, function(t) {});
    },
    getComparisonList: function() {
        var t = wx.getStorageSync("comparisonList");
        return Array.isArray(t) && t ? t : (wx.removeStorageSync("comparisonList"), []);
    },
    deleteComparisonPkgId: function(t) {
        var e = this.getComparisonList(), o = e.indexOf(t);
        -1 == o || e.splice(o, 1), wx.setStorageSync("comparisonList", e);
    },
    addComparisonPkgId: function(t) {
        var e = this.getComparisonList();
        -1 == e.indexOf(t) ? e.length >= 10 ? wx.showToast({
            icon: "none",
            title: "添加失败，最多添加10个套餐去对比！"
        }) : (wx.showToast({
            icon: "none",
            title: "添加成功,点击下方对比,去对比套餐吧!"
        }), e.unshift(t)) : wx.showToast({
            icon: "none",
            title: "该套餐已存在，请勿重复添加!"
        }), wx.setStorageSync("comparisonList", e);
    },
    getHosHistoryList: function() {
        var t = wx.getStorageSync("hosHistoryList");
        return Array.isArray(t) && t ? t : (wx.removeStorageSync("hosHistoryList"), []);
    },
    addHosHistoryWithHosId: function(t) {
        if (t && null != t && "undefined" != t) {
            var e = this, o = new Date().getTime(), n = {
                fid: t,
                date: o
            }, a = [];
            this.getHosHistoryList().forEach(function(t) {
                e.isSameDay(t.date, o) && t.fid === n.fid || a.push(t);
            }), a.unshift(n), wx.setStorageSync("hosHistoryList", a);
        }
    },
    removeHosHistoryList: function() {
        wx.removeStorageSync("hosHistoryList");
    },
    getPkgHistoryList: function() {
        var t = wx.getStorageSync("pkgHistoryList");
        return Array.isArray(t) && t ? t : (wx.removeStorageSync("pkgHistoryList"), []);
    },
    addPkgHistoryWithHosId: function(t) {
        if (t && null != t && "undefined" != t) {
            var e = this, o = new Date().getTime(), n = {
                fid: t,
                date: o
            }, a = [];
            this.getPkgHistoryList().forEach(function(t) {
                e.isSameDay(t.date, o) && t.fid === n.fid || a.push(t);
            }), a.unshift(n), wx.setStorageSync("pkgHistoryList", a);
        }
    },
    removePkgHistoryList: function() {
        wx.removeStorageSync("pkgHistoryList");
    },
    isSameDay: function(t, e) {
        return new Date(t).setHours(0, 0, 0, 0) === new Date(e).setHours(0, 0, 0, 0);
    },
    saveHosAndPkgHistory: function() {
        var t = this, e = this.getHosHistoryList(), o = this.getPkgHistoryList();
        if (0 != e.length || 0 != o.length) {
            var n = {
                hosItems: e,
                pkgItems: o,
                source: this.globalData.appInfo.zkSource
            };
            this.globalData.api.postPort("batchSaveOrUpdateMemberBehavior", n, !1).then(function(e) {
                t.removeHosHistoryList(), t.removePkgHistoryList();
            }, function(t) {});
        }
    },
    pushPage: function(t) {
        var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : 7;
        t && (getCurrentPages().length > e ? wx.redirectTo({
            url: t
        }) : wx.navigateTo({
            url: t
        }));
    },
    onlineKeFuTap: function(t) {
        this.globalData.qykfObj = t, wx.navigateTo({
            url: "/subpackages/online/pages/kf-online"
        });
    },
    callPhoneKeFuTap: function() {
        wx.makePhoneCall({
            phoneNumber: getApp().globalData.tel
        });
    },
    copy: function(t) {
        wx.setClipboardData({
            data: t,
            success: function(t) {
                wx.getClipboardData({
                    success: function(t) {
                        wx.showToast({
                            title: "复制成功"
                        });
                    }
                });
            }
        });
    }
});